import * as types from '../types'
import Im from '../../yf/im/im'
import ChatUtil from '../../yf/im/chat'
 const chat = {
    state:{
        msgMap:{},//用户id为Key msgList为value 所有消息
        curMsgList:[],//当前聊天页消息
        sessionList:[]//会话列表
    },
    mutations:{
        [types.PUSH_MSG](state,msg){
            ChatUtil.pushMsgMap(state.msgMap,state.curMsgList,msg)
        }
    },
    actions:{
        initIm({commit},{userId,groupIds}){
            let path = process.env.VUE_APP_IM_PATH
            Im.init(path,onmessage).then(()=>{
                ChatUtil.sendConnect(userId,groupIds)
                //启动心跳
            }).catch(error=>{
                console.log(error)
            })
        }
    }
}

export default chat

import store from '../../store'

/**
 * 接收消息
 * @param msg
 */
function onmessage(msg) {
  store.commit(types.PUSH_MSG,msg)
}